package com.itsu.admin.config;

import com.itsu.admin.mvc.AdminLoginInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * @author Jerry.Su
 * @Date 2021/12/31 16:07
 */
@Configuration
public class JobAdminConfig {

    @Bean
    public WebMvcConfigurer webMvcConfigurer(AdminLoginInterceptor adminLoginInterceptor) {
        return new WebMvcConfigurer() {
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                registry.addInterceptor(adminLoginInterceptor)
                        .addPathPatterns("/**")
                        .excludePathPatterns("/login", "/auth/**", "/css/**", "/js/**", "/images/**", "/layui/**", "/json/**", "/error/**", "/favicon.ico");
            }

            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/login").setViewName("login");
                registry.addViewController("/index").setViewName("index");
                registry.addViewController("/page/main.html").setViewName("main");
                registry.addViewController("/page/task.html").setViewName("task");
                registry.addViewController("/page/taskAdd.html").setViewName("taskAdd");
                registry.addViewController("/page/node.html").setViewName("node");
                registry.addViewController("/page/dataBak.html").setViewName("dataBak");
                registry.addViewController("/page/taskImp.html").setViewName("taskImp");
            }

            @Override
            public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
                converters.add(0, new MappingJackson2HttpMessageConverter());
            }
        };
    }

    @Bean
    public AdminLoginInterceptor adminLoginInterceptor() {
        return new AdminLoginInterceptor();
    }

}
